package study.itheima.Test22;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(7777);
        //匿名内部类创建多线程
        while (true) {
            Socket s = ss.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    UUID uuid = UUID.randomUUID();
                    String name = uuid.toString();
                    File ff = new File("secondtest",name+".jpg");
                    try (BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
                         BufferedOutputStream bos  =new BufferedOutputStream(new FileOutputStream(ff));
                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()))) {
                        byte[] b = new byte[1024];
                        int len ;
                        while ((len = bis.read(b)) != -1){
                            bos.write(b,0,len);
                            bos.flush();
                        }
                        //给出反馈
                        bw.write("服务器的反馈:文件上传成功");
                        bw.newLine();
                        bw.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
